package org.example.classtopia.Service.Impl;

import org.example.classtopia.Model.dto.AiSessionCreateDTO;
import org.example.classtopia.Model.dto.AiSessionUpdateDTO;
import org.example.classtopia.Model.Entity.AiChatSession;
import org.example.classtopia.Model.vo.AiSessionVO;
import org.example.classtopia.Model.vo.AiSessionListVO;
import org.example.classtopia.Common.Result.Result;
import org.example.classtopia.Service.AiChatService;
import org.example.classtopia.Mapper.AiChatMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.UUID;

@Service
public class AiChatServiceImpl implements AiChatService {
    @Autowired
    private AiChatMapper aiChatMapper;

    @Override
    @Transactional
    public Result<AiSessionVO> createSession(AiSessionCreateDTO dto) {
        String sessionId = UUID.randomUUID().toString();
        AiChatSession session = new AiChatSession();
        session.setId(sessionId);
        session.setTitle(dto.getTitle());
        session.setUserId(1L); // 测试用，确保不为null
        session.setCreatedAt(LocalDateTime.now());
        session.setUpdatedAt(LocalDateTime.now());
        System.out.println("准备插入会话：" + session);
        int inserted = aiChatMapper.insert(session);
        System.out.println("插入结果：" + inserted);
        if (inserted > 0) {
            AiSessionVO vo = new AiSessionVO();
            vo.setId(sessionId);
            vo.setTitle(dto.getTitle());
            vo.setCreatedAt(session.getCreatedAt().toString());
            vo.setUpdatedAt(session.getUpdatedAt().toString());
            return Result.success(vo);
        } else {
            return Result.error("创建会话失败");
        }
    }

    @Override
    public Result<AiSessionVO> getSessionById(String id) {
        return Result.error("未实现");
    }

    @Override
    public Result<AiSessionListVO> getAllSessions(int page, int size) {
        return Result.error("未实现");
    }

    @Override
    public Result<Void> updateSession(AiSessionUpdateDTO dto) {
        return Result.error("未实现");
    }

    @Override
    public Result<Void> deleteSessionById(String id) {
        return Result.error("未实现");
    }
}
